package com.meetingshow.privilege.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.privilege.dao.model.PriUserFollowPO;
import com.meetingshow.privilege.service.IPriUserFollowService;
import com.meetingshow.privilege.service.model.PriUserFollowQueryParas;
import com.meetingshow.privilege.uiservice.model.PriUserFollowQueryParasVO;
import com.meetingshow.privilege.uiservice.model.PriUserFollowVO;
import com.meetingshow.utils.BaseUtil;

public class UIPriUserFollowServiceImp extends BaseUIServiceImp implements IUIPriUserFollowService {
    
    public final static Logger logger = Logger.getLogger(UIPriUserFollowServiceImp.class);

    private IPriUserFollowService priUserFollowService;
    
    /**
     * @return the priUserFollowService
     */
    public IPriUserFollowService getPriUserFollowService() {
        return priUserFollowService;
    }

    /**
     * @param priUserFollowService the priUserFollowService to set
     */
    public void setPriUserFollowService(IPriUserFollowService priUserFollowService) {
        this.priUserFollowService = priUserFollowService;
    }

    @Override
    public Integer savePriUserFollow(PriUserFollowVO priUserFollowVO)
            throws BaseUIServiceException {
        
        if (!BaseUtil.objectNotNull(priUserFollowVO)) {
            this.throwValidaterException("priUserFollowVO is null.");
        }
        
        priUserFollowVO.setCreateTime(new Date());
        
        validateThrowEx(priUserFollowVO);
        
        PriUserFollowQueryParas priUserFollowQueryParas = new PriUserFollowQueryParas();
        priUserFollowQueryParas.setToUserId_e(String.valueOf(priUserFollowVO.getToUserId()));
        priUserFollowQueryParas.setFromUserId_e(String.valueOf(priUserFollowVO.getFromUserId()));
        
        QueryResults<PriUserFollowPO> ufQueryResults = null;
        
        try {
            ufQueryResults = getPriUserFollowService().queryPriUserFollow(priUserFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(ufQueryResults)) {
            logger.error("The user had followed this user before.");
            throw new BaseUIServiceException("The user had followed this user before.");
        }
        
        try {
            Integer id = getPriUserFollowService().savePriUserFollow(priUserFollowVO.toPriUserFollowPO());
            priUserFollowVO.setId(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return priUserFollowVO.getId();
    }


    @Override
    public void removePriUserFollow(PriUserFollowVO priUserFollowVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(priUserFollowVO)) {
            this.throwValidaterException("priUserFollowVO is null.");
        }
        
        validateThrowEx(priUserFollowVO, RemoveValidator.class);
        
        PriUserFollowQueryParas priUserFollowQueryParas = new PriUserFollowQueryParas();
        priUserFollowQueryParas.setId_e(String.valueOf(priUserFollowVO.getId()));
        
        QueryResults<PriUserFollowPO> priUserFollowQueryResults = null;
        try {
            priUserFollowQueryResults = getPriUserFollowService().queryPriUserFollow(priUserFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(priUserFollowQueryResults)) {
            PriUserFollowPO persistPriUserFollowPO = priUserFollowQueryResults.getResultList().get(0);
            try {
                getPriUserFollowService().removePriUserFollow(persistPriUserFollowPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public QueryResults<PriUserFollowVO> queryPriUserFollow(
            PriUserFollowQueryParasVO priUserFollowQueryParasVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(priUserFollowQueryParasVO)) {
            this.throwValidaterException("priUserFollowQueryParasVO is null.");
        }
        
        validateThrowEx(priUserFollowQueryParasVO);
        
        QueryResults<PriUserFollowPO> priUserFollowPOQueryResults = null;
        try {
            priUserFollowPOQueryResults = getPriUserFollowService().queryPriUserFollow(priUserFollowQueryParasVO.toPriUserFollowQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        QueryResults<PriUserFollowVO> priUserFollowVOQueryResults = new QueryResults<PriUserFollowVO>();
        List<PriUserFollowVO> priUserFollowVOList = new ArrayList<PriUserFollowVO>();
        if (QueryResults.resultsListNotNull(priUserFollowPOQueryResults)) {
            for (PriUserFollowPO priUserFollowPO : priUserFollowPOQueryResults.getResultList()) {
                if (BaseUtil.objectNotNull(priUserFollowPO)) {
                    priUserFollowVOList.add(new PriUserFollowVO(priUserFollowPO));
                }
            }
        }
        
        priUserFollowVOQueryResults.setFirstResultNo(priUserFollowPOQueryResults.getFirstResultNo());
        priUserFollowVOQueryResults.setResultsPerGroup(priUserFollowPOQueryResults.getResultsPerGroup());
        priUserFollowVOQueryResults.setResultList(priUserFollowVOList);
        priUserFollowVOQueryResults.setTotalResultsNum(priUserFollowPOQueryResults.getTotalResultsNum());
        
        return priUserFollowVOQueryResults;
    }

}
